package com.hua.springCloud.Service.impl;

import com.hua.springCloud.Service.AccountService;
import com.hua.springCloud.Service.OrderService;
import com.hua.springCloud.Service.StorageService;
import com.hua.springCloud.entities.Order;
import com.hua.springCloud.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;
    @Override
    //这个name可以随便写 只要唯一
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("创建订单开始~~~~~~~~~·");
        orderMapper.create(order);
        //减库存
        log.info("开始减少库存");
        System.out.println(order.getProductId());
        System.out.println(order.getCount());
        storageService.reduce(order.getProductId(),order.getCount());
        log.info("减少库存成功");
        //扣金额
        log.info("开始减少余额");
        accountService.reduceMoney(order.getUserId(),order.getMoney());
        log.info("减少余额成功~~~~~~~~");
        orderMapper.update(order.getUserId(),0);
        log.info("创建订单成功,O(∩_∩)O~~~~~~~~");
    }
}
